<?php
namespace App\Traits;

trait WorkerRedisConnect
{
    private static $connect_timeout;

    private static $host;

    private static $port;

    private static $password;

    private static $username;

    public static function connectWorker()
    {
        self::init();
        $redis = new \Redis();
        $redis->connect(self::$host, self::$port);
        if(self::$password) {
            $redis->auth(self::$password);
        }
        $redis->select(1);//选择1库
        return $redis;
    }

    public static function init()
    {
        self::host($_ENV['REDIS_HOST']);
        self::port($_ENV['REDIS_PORT']);
        self::password(isset($_ENV['REDIS_PASSWORD'])?$_ENV['REDIS_PASSWORD']:null);
    }

    public static function host($host)
    {
        self::$host = $host;
    }

    public static function port($port)
    {
        self::$port = $port;
    }

    public static function password($password)
    {
        if($password != 'null' || !$password)
            self::$password = $password;
    }
}
